<?php

// copyright (c) http://ShopUpgraded.com 2010
// copyright (c) Philip Clarke, Bouncing Ltd.

class shopupgraded_query_cache{

    var $su_product_cache = array();
    var $su_specials_cache = array();
    var $su_sales_cache = array();
    var $su_tax_rates = array();

  function shopupgraded_query_cache(){
    $this->su_products_details_cache(null) ;
  }

  function su_products_details_cache($products_id){

    global $db;

    if(sizeof($this->su_product_cache)==0){

      $sql = "select
		products_id,
		products_tax_class_id,
		products_price,
		products_priced_by_attribute,
		product_is_free, product_is_call,
		products_type,
		master_categories_id,
		products_quantity_order_min,
		products_quantity_order_max,
		products_quantity_order_units,
		products_weight,
		product_is_always_free_shipping,
		products_discount_type,
		products_discount_type_from,
		products_virtual,
		products_model,
		products_quantity_mixed,
		products_qty_box_status
	    from `" . TABLE_PRODUCTS . "`" ;

      $product = $db->Execute($sql);

      while(!$product->EOF){
	$this->su_product_cache[$product->fields['products_id']]->fields = $product->fields ;
	$product->MoveNext();
      }
    }


    if(isset($this->su_product_cache[(int)$products_id])){
      return $this->su_product_cache[(int)$products_id] ;
    }
      return false;

  }


  function su_specials_get_cached($products_id){

    global $db;

      if(sizeof($this->su_specials_cache)==0){
	$specials = $db->Execute("select products_id, specials_new_products_price from " . TABLE_SPECIALS . " where status='1'");
	  while(!$specials->EOF){
	    $this->su_specials_cache[$specials->fields['products_id']]->fields = $specials->fields ;
	    $specials->MoveNext();
	  }
      }
      if(isset($this->su_specials_cache[(int)$products_id])){
	return $this->su_specials_cache[(int)$products_id] ;
      }else{
	return false;
      }

  }


function su_cache_get_tax_rate($class_id, $country_id = -1, $zone_id = -1) {

    global $db;

    $key = ((int)$class_id.'_'.(int)$country_id.'_'.(int)$zone_id);


    if(!isset($this->su_tax_rates[$key])){

    $tax_query = "select sum(tax_rate) as tax_rate
                  from (" . TABLE_TAX_RATES . " tr
                  left join " . TABLE_ZONES_TO_GEO_ZONES . " za on (tr.tax_zone_id = za.geo_zone_id)
                  left join " . TABLE_GEO_ZONES . " tz on (tz.geo_zone_id = tr.tax_zone_id) )
                  where (za.zone_country_id is null
                  or za.zone_country_id = 0
                  or za.zone_country_id = '" . (int)$country_id . "')
                  and (za.zone_id is null
                  or za.zone_id = 0
                  or za.zone_id = '" . (int)$zone_id . "')
                  and tr.tax_class_id = '" . (int)$class_id . "'
                  group by tr.tax_priority";


      $tax = $db->Execute($tax_query);


	$this->su_tax_rates[$key]->rc = $tax->RecordCount() ;
	while(!$tax->EOF){
	  $this->su_tax_rates[$key]->cache[]->fields = $tax->fields ;
	  $tax->MoveNext();
	}

    }

    if(isset($this->su_tax_rates[$key])){
      return $this->su_tax_rates[$key] ;
    }

    return false;

}

  function su_sales_get_cached($category, $product_price){

    global $db;

      $key = ($category.'_'.$product_price);

      if(!isset($this->su_specials_cache[$key])){

	$sale_query = "select sale_specials_condition, sale_deduction_value, sale_deduction_type, sale_categories_all from " . TABLE_SALEMAKER_SALES . " where sale_categories_all like '%," . $category . ",%' and sale_status = '1' and (sale_date_start <= now() or sale_date_start = '0001-01-01') and (sale_date_end >= now() or sale_date_end = '0001-01-01') and (sale_pricerange_from <= '" . $product_price . "' or sale_pricerange_from = '0') and (sale_pricerange_to >= '" . $product_price . "' or sale_pricerange_to = '0')";

	$sale = $db->Execute($sale_query);


	  $this->su_specials_cache[$key]->rc = $sale->RecordCount() ;
	  while(!$sale->EOF){
	    $this->su_specials_cache[$key]->cache[]->fields = $sale->fields ;
	    $sale->MoveNext();
	  }

      }

      if(isset($this->su_specials_cache[$key])){
	return $this->su_specials_cache[$key] ;
      }

      return false;


  }

} // end of object

  $su_cached = new shopupgraded_query_cache;

